#include "contactbutton.h"

BEGIN_EVENT_TABLE(ContactButton, wxPanel)
    EVT_MOTION(ContactButton::mouseMoved)
    EVT_LEFT_DOWN(ContactButton::mouseDown)
    EVT_LEFT_UP(ContactButton::mouseReleased)
    EVT_RIGHT_DOWN(ContactButton::rightClick)
    EVT_LEAVE_WINDOW(ContactButton::mouseLeftWindow)
    EVT_KEY_DOWN(ContactButton::keyPressed)
    EVT_KEY_UP(ContactButton::keyReleased)
    EVT_MOUSEWHEEL(ContactButton::mouseWheelMoved)
    EVT_PAINT(ContactButton::paintEvent)
    EVT_LEFT_DCLICK(ContactButton::doubleClick)
END_EVENT_TABLE()

ContactButton::ContactButton(ScrolledWidgetsPane* parent, wxString nickname)
    :wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize)
{
//#ifndef WINDOWS
    SetMinSize(wxSize(buttonWidth, buttonHeight));
    this->nickname = nickname;
    color = false;
    getParent()->checked = NULL;
    pressedDown = false;
//#endif
}

ScrolledWidgetsPane* ContactButton::getParent(){ return dynamic_cast<ScrolledWidgetsPane*>(this->GetParent()); };

void ContactButton::paintEvent(wxPaintEvent & evt){
    wxPaintDC dc(this);
    wxFont font = wxFont(8, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    font.SetFaceName(wxT("Arial"));
    dc.SetFont(font);
    dc.DrawText(nickname, wxPoint(27, 9));
    dc.DrawBitmap(Bitmaps::bitmap[GG_STATUS_AVAIL], wxPoint(5, 5));
}

void ContactButton::paintNow(ContactButton* btn, bool doubleclick, bool uncheck){
    if(btn){
        if(doubleclick)
            btn->color = false;

        if(uncheck)
            btn->color = true;

        if(btn->color)
            btn->SetBackgroundColour(wxNullColour);
        else
            btn->SetBackgroundColour(*wxLIGHT_GREY);

        btn->color = !btn->color;
        btn->Refresh();
    }
}

void ContactButton::mouseDown(wxMouseEvent& event){
    ScrolledWidgetsPane* parent = getParent();

    if(parent->checked != this)
        paintNow(parent->checked, false, true);

    paintNow(this);
    redraw(wxT("Redraw!"));
    parent->checked = this;
}

void ContactButton::doubleClick(wxMouseEvent& event){
    ScrolledWidgetsPane* parent = getParent();

    if(parent->checked != this)
        paintNow(parent->checked, false, true);

    paintNow(this, true);
    parent->checked = this;
    wxMessageBox(wxT("DoubleClick on ") + nickname + wxT("!"));
}

void ContactButton::mouseMoved(wxMouseEvent& event){ event.Skip(); }
void ContactButton::mouseWheelMoved(wxMouseEvent& event){ event.Skip(); }
void ContactButton::rightClick(wxMouseEvent& event){ event.Skip(); }
void ContactButton::keyPressed(wxKeyEvent& event){ event.Skip(); }
void ContactButton::keyReleased(wxKeyEvent& event){ event.Skip(); }
void ContactButton::mouseReleased(wxMouseEvent& event){ event.Skip(); }
void ContactButton::mouseLeftWindow(wxMouseEvent& event){ event.Skip(); }

void ContactButton::redraw(wxString nickname){
    this->nickname = nickname;

    Connect(wxEVT_PAINT, wxPaintEventHandler(ContactButton::paintEvent), NULL, this);
}
